public class centerOfMassFinder {
	public double x1, y1, x2, y2, x3, y3, x4, y4;
	public double centerX, centerY;
	public double[] center = new double[2];
	public centerOfMassFinder(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		this.x3 = x3;
		this.y3 = y3;
		this.x4 = x4;
		this.y4 = y4;
	}

	public void findCenterOfMass() {
		double[] center1, center2, center3, center4;
		center1 = centerOfTriangle(x1, y1, x2, y2, x3, y3);
		center2 = centerOfTriangle(x1, y1, x3, y3, x4, y4);
		center3 = centerOfTriangle(x2, y2, x3, y3, x4, y4);
		center4 = centerOfTriangle(x4, y4, x1, y1, x2, y2);
		center = findIntersectionOfLines(center1, center2, center3, center4);
                centerX = center[0];
                centerY = center[1];
	}

	public double[] centerOfTriangle(double x1, double y1, double x2, double y2, double x3, double y3) {
		double[] center = new double[2];
		center[0] = (x1 + x2 + x3) / 3;
		center[1] = (y1 + y2 + y3) / 3;
		return center;
	}

	public double[] findIntersectionOfLines(double[] p1, double[] p2, double[] p3, double[] p4) {
		double[] intersection = new double[2];

		double h1 = (((p1[0] * p2[1]) - (p1[1] * p2[0])) * (p3[0] - p4[0]));
		double h2 = ((p1[0] - p2[0]) * ((p3[0] * p4[1]) - (p3[1] * p4[0])));
		double h3 = (((p1[0] - p2[0]) * (p3[1] - p4[1])) - ((p1[1] - p2[1]) * (p3[0] - p4[0])));
		double x = (h1 - h2) / h3;

		double k1 = (((p1[0] * p2[1]) - (p1[1] * p2[0])) * (p3[1] - p4[1]));
		double k2 = ((p1[1] - p2[1]) * ((p3[0] * p4[1]) - (p3[1] * p4[0])));
		double k3 = (((p1[0] - p2[0]) * (p3[1] - p4[1])) - ((p1[1] - p2[1]) * (p3[0] - p4[0])));
		double y = (k1 - k2) / k3;

		intersection[0] = x;
		intersection[1] = y;

		return intersection;
	}
}
